package com.yxq.mdt.app.modules.spikeorder.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.order.bean.EventOrderMsg;
import com.yxq.mdt.app.modules.order.bean.OrderConfirmBean;
import com.yxq.mdt.app.modules.order.bean.OrderPayResultWxBean;
import com.yxq.mdt.app.modules.spikeorder.bean.ExpressBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderDetailBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderListBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderPayResultBalanceBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderRefundDetailBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderSubmitResult;
import com.yxq.mdt.app.modules.spikeorder.bean.SpikeOrderConfirmBean;
import com.yxq.mdt.app.modules.spikeorder.model.OrderStatusUtils;
import com.yxq.mdt.app.modules.spikeorder.model.SpikeOrderModel;
import com.yxq.mdt.app.modules.spikeorder.prestener.ISpikeOrderContract;
import com.yxq.mdt.app.modules.spikeorder.prestener.SpikeOrderPresenter;
import com.yxq.mdt.app.net.ApiModel;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.SinglePicker;

/**
 * @author L
 * @time
 * @describe 售后订单详情
 */
public class SpikeOrderRefundDetailActivity extends BaseActivity<SpikeOrderPresenter> implements ISpikeOrderContract.View {


    @BindView(R.id.order_detail_tv_status)
    TextView orderDetailTvStatus;
    @BindView(R.id.order_tv_number)
    TextView orderTvNumber;
    @BindView(R.id.order_tv_date)
    TextView orderTvDate;
    @BindView(R.id.order_express_layout)
    RelativeLayout orderExpressLayout;
    @BindView(R.id.order_confirm_goods_list_iv_img)
    ImageView orderConfirmGoodsListIvImg;
    @BindView(R.id.cart_tv_name)
    TextView cartTvName;
    @BindView(R.id.cart_tv_price)
    TextView cartTvPrice;
    @BindView(R.id.cart_tv_attr)
    TextView cartTvAttr;
    @BindView(R.id.hint)
    TextView hint;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.order_detail_tv_all_price_hint)
    TextView orderDetailTvAllPriceHint;
    @BindView(R.id.order_detail_tv_all_price)
    TextView orderDetailTvAllPrice;
    @BindView(R.id.order_detail_tv_all_num_hint)
    TextView orderDetailTvAllNumHint;
    @BindView(R.id.order_detail_tv_all_num)
    TextView orderDetailTvAllNum;
    @BindView(R.id.order_detail_tv_all_delivery_hint)
    TextView orderDetailTvAllDeliveryHint;
    @BindView(R.id.order_detail_tv_all_delivery)
    TextView orderDetailTvAllDelivery;
    @BindView(R.id.address_tv_name)
    EditText addressTvName;
    @BindView(R.id.address_tv_phone)
    EditText addressTvPhone;
    @BindView(R.id.address_tv_address)
    EditText addressTvAddress;
    @BindView(R.id.order_address_layout)
    RelativeLayout orderAddressLayout;
    @BindView(R.id.order_iv_express_name)
    TextView orderIvExpressName;
    @BindView(R.id.order_btn_submit)
    Button orderBtnSubmit;
    @BindView(R.id.order_tv_express_company)
    TextView orderTvExpressCompany;
    @BindView(R.id.order_tv_express_no)
    EditText orderTvExpressNo;
    @BindView(R.id.order_express_edit_layout)
    RelativeLayout orderExpressEditLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_refund_detail;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new SpikeOrderPresenter(this, new SpikeOrderModel());
        SpikeOrderModel.refundDetail(getIntent().getStringExtra("orderId"), new ApiModel.CommonCallback<OrderRefundDetailBean>() {
            @Override
            public void onSuccess(OrderRefundDetailBean orderRefundDetailBean) {
                bindUI(orderRefundDetailBean);
            }

            @Override
            public void onError(String errMsg) {
                ToastUtils.showShort(errMsg);
            }
        });
    }

    /**
     * 填充数据到界面
     *
     * @param orderRefundDetailBean
     */
    private void bindUI(OrderRefundDetailBean orderRefundDetailBean) {
        String status = OrderStatusUtils.refundStatus(orderRefundDetailBean.getIs_agree(), orderRefundDetailBean.getIs_user_send(), orderRefundDetailBean.getRefund_status());
        orderDetailTvStatus.setText(status);
        if (status.equals("等待买家发货")) {
            //显示发货按钮
            orderExpressLayout.setVisibility(View.GONE);
            orderBtnSubmit.setVisibility(View.VISIBLE);
            orderExpressEditLayout.setVisibility(View.VISIBLE);
            orderAddressLayout.setVisibility(View.VISIBLE);
        } else if (orderRefundDetailBean.getIs_user_send() == 1) {
            //已发货 显示单号
            orderExpressLayout.setVisibility(View.VISIBLE);
            orderBtnSubmit.setVisibility(View.GONE);
            orderExpressEditLayout.setVisibility(View.GONE);
            orderAddressLayout.setVisibility(View.GONE);
        } else {
            orderExpressLayout.setVisibility(View.GONE);
            orderBtnSubmit.setVisibility(View.GONE);
            orderExpressEditLayout.setVisibility(View.GONE);
            orderAddressLayout.setVisibility(View.GONE);
        }

        orderTvNumber.setText("快递公司：" + orderRefundDetailBean.getUser_send_express());
        orderTvDate.setText("快递单号：" + orderRefundDetailBean.getUser_send_express_no());
        GlideHelper.setLoadRoundImage(this, orderRefundDetailBean.getGoods_pic(), 0, orderConfirmGoodsListIvImg);
        cartTvName.setText(orderRefundDetailBean.getName());
        tvNum.setText(orderRefundDetailBean.getNum());
        orderDetailTvAllPrice.setText(orderRefundDetailBean.getRefund_type() == 1 ? "退款退货" : "换货");
        orderDetailTvAllNum.setText(String.format("￥%.2f", orderRefundDetailBean.getRefund_price()));
        orderDetailTvAllDelivery.setText(orderRefundDetailBean.getRefund_desc() + "");
        cartTvAttr.setText((orderRefundDetailBean.getAttr() != null && orderRefundDetailBean.getAttr().size() > 0) ? orderRefundDetailBean.getAttr().get(0).getAttr_name() : "规格：默认");
        orderIvExpressName.setOnClickListener(view -> showExpressListDialog(orderRefundDetailBean.getExpress_list()));
    }

    @Override
    public void onLoadSuccess(SpikeOrderConfirmBean orderConfirmBean) {

    }

    @Override
    public void onLoadFailed(String err) {

    }

    @Override
    public void onLoadListSuccess(OrderListBean orderListBean) {

    }

    @Override
    public void onLoadListFailed(String err) {

    }

    @Override
    public void onSubmitSuccess(OrderSubmitResult orderSubmitResult) {

    }

    @Override
    public void onSubmitFailed(String err) {

    }

    @Override
    public void onPayBalanceSuccess(OrderPayResultBalanceBean orderPayResultBalanceBean) {

    }

    @Override
    public void onPayBalanceFailed(String err) {

    }

    @Override
    public void onPayWxSuccess(OrderPayResultWxBean orderPayResultWxBean) {

    }

    @Override
    public void onPayWxFailed(String err) {

    }

    @Override
    public void onDetailSuccess(OrderDetailBean orderDetailBean) {

    }

    @Override
    public void onDetailFailed(String err) {

    }

    @Override
    public void onExpressSuccess(ExpressBean expressBean) {

    }

    @Override
    public void onExpressFailed(String err) {

    }

    @OnClick({R.id.order_iv_express_name, R.id.order_btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.order_iv_express_name:
//                showExpressListDialog();
                break;
            case R.id.order_btn_submit:
                submit();
                break;
        }
    }

    private OrderRefundDetailBean.ExpressListBean expressListBean;

    /**
     * 物流列表弹出框
     */
    private void showExpressListDialog(List<OrderRefundDetailBean.ExpressListBean> expressListBeanList) {
        SinglePicker singlePicker = new SinglePicker(this, expressListBeanList);
        /** 标题区域 **/
        singlePicker.setTopBackgroundColor(getResources().getColor(R.color.white));//设置标题背景颜色
        singlePicker.setTopHeight(60);//设置顶部标题栏高度（单位为dp）

        singlePicker.setCancelText("取消");
        singlePicker.setCancelTextSize(14);
        singlePicker.setCancelTextColor(getResources().getColor(R.color.color_font_black));//设置顶部标题栏取消按钮文字颜色
        singlePicker.setCancelVisible(true);//设置左边按钮是否显示

        singlePicker.setSubmitText("完成");//设置标题右边文字
        singlePicker.setSubmitTextSize(14);//设置完成按钮大小
        singlePicker.setSubmitTextColor(getResources().getColor(R.color.color_font_black));//设置完成按钮颜色

        /**  标题与列表的分割线 **/
        singlePicker.setTopLineColor(getResources().getColor(R.color.white));
        singlePicker.setTopLineVisible(true);
        singlePicker.setTopLineHeight(10);

        /** 列表区域 **/
        singlePicker.setTextSize(14);//字体大小
        singlePicker.setTextColor(getResources().getColor(R.color.black));//设置省市县字体滚动颜色
        singlePicker.setDividerColor(getResources().getColor(R.color.white));//设置分割线的颜色

        if (expressListBean != null) {
            singlePicker.setSelectedItem(expressListBean);
        }
        singlePicker.show();
        singlePicker.setOnItemPickListener((SinglePicker.OnItemPickListener<OrderRefundDetailBean.ExpressListBean>) (index, item) -> {
            expressListBean = item;
            orderTvExpressCompany.setText(item.getName());
        });
    }

    /**
     * 确认发货
     */
    private void submit() {
        if (expressListBean == null) {
            ToastUtils.showShort("请选择物流公司");
            return;
        }
        if (orderTvExpressNo.getText().toString().length() == 0) {
            ToastUtils.showShort("请输入物流单号");
            return;
        }
        showLoadingDialog();
        String express = expressListBean.getName();
        String express_no = orderTvExpressNo.getText().toString();
        SpikeOrderModel.refundSend(getIntent().getStringExtra("orderId"), express, express_no, "STORE", new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                hideLoadingDialog();
                EventBus.getDefault().post(new EventOrderMsg(5));
                ToastUtils.showShort(s);
                finish();
            }

            @Override
            public void onError(String errMsg) {
                hideLoadingDialog();
                ToastUtils.showShort(errMsg);
            }
        });
    }
}
